<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Comments extends Model
{
    protected $table = 'comments';
    protected $fillable = [
        'video_id', 'content', 'userid', 'replay_id', 'praise', 'created_at','add_at'
    ];
    public $timestamps = false;//关闭自动维护
    protected $dates = ['created_at','add_at'];

    public function user()
    {
        return $this->belongsTo('App\Models\Users', 'userid', 'id');
    }

    public function replay()
    {
        return $this->belongsTo('App\Models\Users', 'replay_id', 'id');
    }

    public function video()
    {
        return $this->belongsTo('App\Models\Videos', 'video_id', 'id');
    }

    public function getCreatedAtAttribute($value)
    {
        $now = time();
        $time = Carbon::parse($value)->timestamp;
        $t = $now - $time;
        if ($t < 60) {
            $str = '刚刚';
        } elseif ($t < 60 * 60) {
            $min = floor($t / 60);
            $str = $min . '分钟前';
        } elseif ($t < 60 * 60 * 24) {
            $h = floor($t / (60 * 60));
            $str = $h . '小时前';
        } elseif ($t < 60 * 60 * 24 * 3) {
            $d = floor($t / (60 * 60 * 24));
            $str = $d . '天前';
        } else {
            $str = Carbon::parse($value)->format('Y-m-d');
        }
        return $str;
    }


    public static function boot() {
        parent::boot();
        #只添加created_at不添加updated_at
        static::creating(function ($model) {
            $model->created_at = $model->freshTimestamp();
            //$model->updated_at = $model->freshTimeStamp();
        });
    }


}
